//
//  CollectionViewController.m
//  collectionViewDemo
//
//  Created by Cluy on 14/12/22.
//  Copyright (c) 2014年 sunflows. All rights reserved.
//

#import "CollectionViewController.h"
#import "GoodsCollectionCell.h"
#import "CollectionInfoViewController.h"

@interface CollectionViewController ()

@end

@implementation CollectionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _listArr = [NSMutableArray array];
    _goodsCollectionView.delegate = self;
    _goodsCollectionView.dataSource = self;

    [self.goodsCollectionView registerNib:[UINib nibWithNibName:@"GoodsCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"GoodsCollectionCell"];
    
    int i = 30/9;
    
    //设置
    self.pageCtr.numberOfPages=i; //设置页数为2
    self.pageCtr.currentPage=0; //初始页码为 0
    self.pageCtr.userInteractionEnabled=NO; //pagecontroller不响应点击操作
    self.pageCtr.selected = YES;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- ScrollerView

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGPoint offset = scrollView.contentOffset;
    self.pageCtr.currentPage = offset.x /1000; //计算当前的页码
}


#pragma mark -- UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 30;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"GoodsCollectionCell";
    GoodsCollectionCell * cell = (GoodsCollectionCell *)[_goodsCollectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    NSString *str = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    [_listArr addObject:str];
    cell.goodsTitle.text = str;
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//
////定义每个UICollectionView 的大小
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return CGSizeMake(250   , 250 );
//                     
//}
//
////定义每个UICollectionView 的 margin
//-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(15, 15, 15, 15);
//}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    CollectionInfoViewController *collectInfoVC = [[CollectionInfoViewController alloc]init];
    collectInfoVC.listArr = self.listArr;
    collectInfoVC.num = (int)indexPath.row;
    [self presentViewController:collectInfoVC animated:YES completion:^{
        
    }];
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

@end
